<table class="layui-table" id="infotable">
    <tr>
        <td class="tb_title">机器人：</td>
        <td><span id="robot_id"></span></td>
        <td class="tb_title">网络地址：</td>
        <td><span id="robot_ip"></span></td>
        <td class="tb_title">位置：</td>
        <td><span id="robot_class"></span></td>
    </tr>
    <tr>
        <td class="tb_title" id="order_type">采购单号：</td>
        <td><span id="order_no"></span></td>
        <td class="tb_title">接收日期：</td>
        <td><span id="order_date"></span></td>
        <td class="tb_title">供应商：</td>
        <td><span id="order_supper"></span></td>
    </tr>
    <tr>
        <td class="tb_title">产品清单：</td>
        <td colspan="5" style="width: 120px">
            <span id="order_product" style="display: block;height: 20px;overflow-wrap: break-word"></span>
        </td>
    </tr>
</table>

<script type='text/javascript'>
    function loadOrder(orderid){
        var action = $("#mm").attr("action")
        var sql = "select po.id,po.name,po.date_planned,po.state,rp.display_name "+
                ",(select string_agg(pt.name||'('||product_qty||'/'||qty_received||')',',') from purchase_order_line ol" +
                " inner join product_product pp on ol.product_id = pp.id " +
                " inner join product_template pt on pt.id = pp.product_tmpl_id where ol.order_id = po.id) "+
                " from purchase_order po  "+
                " LEFT JOIN res_partner rp on po.partner_id = rp.id LEFT JOIN res_users ru on ru.id = po.user_id ";
        if(orderid.startsWith("P")){
            sql = sql + " where po.name='"+orderid+"'"
        }else{
            sql = sql + " where po.id = "+orderid
        }
        $.post(action,{sql:sql},function(result){
            result = JSON.parse(result);
            if(!result||result.length==0){
                location.href = "supplierList.html"
                return;
            }
            var pname = result[0][1]
            var date = result[0][2]
            var product = result[0][5]
            var supper = result[0][4]
            $("#order_no").html(pname)
            $("#order_date").html(eosFormatTime(date))
            $("#order_product").html(product)
            $("#order_supper").html(supper)
        });
    }

    function loadRev(revname){
        var action = $("#mm").attr("action")
        var sql = "select po.id,po.name,po.date_planned,po.state,rp.display_name "+
                ",(select string_agg(pt.name||'('||product_qty||'/'||qty_received||')',',') from purchase_order_line ol "+
                " inner join product_product pp on ol.product_id = pp.id " +
                " inner join product_template pt on pt.id = pp.product_tmpl_id where ol.order_id = po.id) "+
                " from purchase_order po LEFT JOIN res_partner rp on po.partner_id = rp.id LEFT JOIN res_users ru on ru.id = po.user_id " +
                " Left JOIN stock_picking sp on po.name = sp.origin where sp.name = '"+revname+"'"
        $.post(action,{sql:sql},function(result){
            result = JSON.parse(result);
            if(!result||result.length==0){
                location.href = "supplierList.html"
                return;
            }
            var pname = result[0][1]
            var date = result[0][2]
            var product = result[0][5]
            var supper = result[0][4]
            $("#order_no").html(revname)
            $("#order_type").html("接收单号：");
            $("#order_date").html(eosFormatTime(date))
            $("#order_product").html(product)
            $("#order_supper").html(supper)
        });
    }

    function loadAsn(asncode){
        var action = $("#mm").attr("action")
        var sql = "select ba.asn_code,ba.act_arrival_time,rp.display_name,"+
                    " (select (string_agg(pt.name||'('||bad.item_quantity||')',','))"+
                    " from byjr_asn baa left join byjr_asn_detail bad on baa.id = bad.asn_id"+
                    " left join product_product pp on pp.id = bad.product_id"+
                    " left join product_template pt on pp.product_tmpl_id = pt.id"+
                    " where baa.id = ba.id) from byjr_asn ba "+
                    " left join res_partner rp on rp.id = ba.supplier_id"+
                    " where ba.asn_code ='"+asncode+"'"
        $.post(action,{sql:sql},function(result){
            result = JSON.parse(result);
            if(!result||result.length==0){
                location.href = "supplierList.html"
                return;
            }
            var date = result[0][1]
            var product = result[0][3]
            var supper = result[0][2]
            $("#order_no").html(asncode)
            $("#order_type").html("ASN号：");
            $("#order_date").html(eosFormatTime(date))
            $("#order_product").html(product)
            $("#order_supper").html(supper)
        });
    }

    function initData(){
        setSession("robot_id",param)
        var orderid = "{{getSession('orderid')}}";
        var revname = "{{getSession('revname')}}";
        var asncode = "{{getSession('asncode')}}";
        if(!asncode&&!revname&&!orderid){
            location.href = "supplierList.html?data="+param
            return;
        }
        if(orderid){
            loadOrder(orderid);
            return;
        }
        if(revname){
            loadRev(revname);
            return;
        }
        if(asncode){
            loadAsn(asncode);
            return;
        }
    }

    function select(v) {
        if(v=='TPSJ'){
            document.getElementById('v').value = "";
            setTimeout(function(){
                location.href = "message.html?data="+param+",请扫描容器号"
            },1000);
        }else if(v=='IQCP'){
            // document.getElementById('v').value = "";
            // setTimeout(function(){
            // 	location.href = "supplierList.html?data="+param
            // },1000);
        }else if(v=='IQCC'){
            document.getElementById('v').value = "";
            var orderid = $("#order_no").html();
            var action = $("#mm").attr("action")
            var sql = "select id from byjr_requestpool where request_type = 'IQCC' and order_id = '"+orderid+"' limit 1"
            $.post(action,{sql:sql},function(result){
                result = JSON.parse(result);
                if(!result||result.length==0){
                    var id = "{{getId('byjr_requestpool_id_seq')}}"
                    var sql = "INSERT INTO byjr_requestpool(id,request_type,request_user,request_local,order_id,request_status,request_time)values("+id+",'IQCC','"+robotid+"','0','"+orderid+"','','now()')"
                    $.post(action,{sql:sql},function(result){
                        layer.msg('请求发送成功');
                        setTimeout(function(){
                            // location.href = "show.html?data="+id+",来料检验结论请求"
                            open("来料检验结论","show.html?data="+id+",来料检验结论请求")
                        },1000)
                    });
                }else{
                    id = result[0][0];
                    setTimeout(function(){
                        // location.href = "show.html?data="+id+",来料检验结论请求"
                        open("来料检验结论","show.html?data="+id+",来料检验结论请求")
                    },1000)
                }
            });
        }
    }
</script>